package cn.jtool.dig.server.comm.tunnel.protocol;

import cn.jtool.dig.common.model.Commands;
import cn.jtool.dig.common.model.Packet;
import cn.jtool.dig.common.model.Session;
import cn.jtool.dig.common.netty.CtxUtils;
import cn.jtool.dig.common.netty.ProtocolProcess;
import cn.jtool.dig.common.netty.SessionManager;
import cn.jtool.dig.server.comm.tunnel.TunnelAttrKey;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class P_11_Hearbeat implements ProtocolProcess {

    @Override
    public int protocolId() {
        return Commands.HEARBEAT.getCode();
    }

    @Override
    public Packet process(ChannelHandlerContext ctx, Packet packet) {
        Long sessionId = CtxUtils.getAttribute(ctx, TunnelAttrKey.SESSION_ID);
        if(Objects.nonNull(sessionId)){
            Session session = SessionManager.getSession(sessionId);
            session.setHeartbeatTime(System.currentTimeMillis());
        }
        return null;
    }
}
